package com.zhuwhao.sample.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.customtabs.CustomTabsIntent;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;

import com.zhuwhao.sample.R;
import com.zhuwhao.sample.WebViewActivity;
import com.zhuwhao.sample.customtabs.CustomTabActivityHelper;

/**
 * Created by zhuwh on 2016/12/10 0010.
 * AppUtils
 */
public class AppUtils {

    /**
     * 显示Toast
     *
     * @param context Context
     * @param content 内容
     */
    public static void showToast(Context context, String content) {
        Toast.makeText(context, content, Toast.LENGTH_SHORT).show();
    }

    /**
     * 打开URL，如果手机安装了Chrome将打开自定义标签，否则打开WebActivity
     *
     * @param activity activity
     * @param url      url
     */
    public static void openUrl(final Activity activity, final String url) {
        try {
            CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
            builder.setToolbarColor(ContextCompat.getColor(activity, R.color.colorPrimary));
            builder.setShowTitle(true);
            CustomTabActivityHelper.openCustomTab(activity, builder.build(), Uri.parse(url), new CustomTabActivityHelper.CustomTabFallback() {
                @Override
                public void openUri(Activity activity, Uri uri) {
                    Intent intent = new Intent(activity, WebViewActivity.class);
                    intent.putExtra("url", url);
                    activity.startActivity(intent);
                }
            });
        } catch (Exception e) {
            Intent intent = new Intent(activity, WebViewActivity.class);
            intent.putExtra("url", url);
            activity.startActivity(intent);
        }
    }
}